using Digitalmes.Domain.Entities.Mes.Processes;

namespace Digitalmes.Application.Mes.Processes.Routings.Queries;

/// <summary>
/// 工艺路线查询命令。
/// </summary>
/// <param name="LineId">产线 Id</param>
/// <param name="ProductId">产品 Id</param>
public sealed record GetRoutingListQuery(long? LineId, long? ProductId) : IRequest<IResult<List<ProcRoutingDto>>>;

internal sealed class GetRoutingListQueryHandler(IRepository<ProcRouting> repo) : IRequestHandler<GetRoutingListQuery, IResult<List<ProcRoutingDto>>>
{
    public async Task<IResult<List<ProcRoutingDto>>> Handle(GetRoutingListQuery request, CancellationToken cancellationToken)
    {
        var items = await repo.AsQueryable()
            .WhereIF(request.LineId > 0, m => m.LineId == request.LineId)
            .WhereIF(request.ProductId > 0, m => m.ProductId == request.ProductId)
            .OrderBy(m => m.Order)
            .ToListAsync(cancellationToken);
        return Result.From(items.Adapt<List<ProcRoutingDto>>());
    }
}
